package me.like.diandian.activity;

import android.os.Bundle;
import android.text.TextUtils;

import com.bumptech.glide.Glide;

import java.util.concurrent.TimeUnit;

import me.like.diandian.R;
import me.like.diandian.api.Api;
import me.like.diandian.base.BaseActivity;
import me.like.diandian.bean.StartImgBean;
import me.like.diandian.databinding.ActivityLaunchBinding;
import me.like.diandian.util.RxUtils;
import me.like.diandian.util.SPUtils;

/**
 * Created by shousakai on 2019/6/22.
 */

public class LaunchActivity extends BaseActivity<ActivityLaunchBinding> {


    @Override
    protected int getLayoutId() {
        return R.layout.activity_launch;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StartImgBean startImg = SPUtils.getStartImg(this);
        if (startImg != null) {
            Glide.with(this).load(startImg.getPicUrl()).into(binding.ivLaunch);
        }
        Api.getInstance().getStartImg().compose(RxUtils.applySchedulerAndAllFilter(this))
                .subscribe(data -> {
                    if (data.getData() != null) {
                        SPUtils.setStartImg(this, data.getData());
                        Glide.with(this).load(data.getData().getPicUrl()).into(binding.ivLaunch);
                    }
                }, Throwable::printStackTrace);

        toJump();

    }
    private void toJump() {
        RxUtils.delayDo(1, TimeUnit.SECONDS, (result, data) -> {
            if (!TextUtils.isEmpty(SPUtils.getToken(this))) {
                MainActivity.launch(this);
            } else {
                LoginActivity.launch(this, false);
            }
            this.finish();
        });
    }
}
